<?php

class i18n_labels extends model
{
	protected $table = 'i18n_labels';
	
	public function tags_by( $p = null )
	{
		if( !is_array( $p ) && ( !is_string( $p ) ) ) throw new Exception( "wrong where in ".__METHOD__ );
		if( is_null( $this ->table ) ) throw new Exception( "internal error bqz table" );
		
		// where
		if( is_array( $p ) ) {
			$p = array_merge( $p, $this ->__fixedFields );
			$_sp = $this ->db ->make_set_list( $p, 'AND' );
		} else {
			$_sp = $this ->db ->make_set_list( $this ->__fixedFields, 'AND' );
			bc::log( $p, 'in' );
			$p = join( ' AND ', array_filter( array_map( 'trim', explode( 'AND', $p ) ) ) );
			bc::log( $p, 'modified' );
			$_sp .= ( $_sp ) ? ' AND ( '.$p.' )' : $p;
		}
		if( !$_sp ) $_sp = 'TRUE';
		$_sp = ' '.$_sp.' ';
		
		
		$sql = sprintf( "SELECT * FROM %s WHERE %s GROUP BY tag;", $this ->table, $_sp );
		
		
		$_r = $this ->db ->_list( $sql, 'tag' );
		$_r = array_filter( $_r );
		
		if( empty( $_r ) ) return $_r;
		
		return $_r;
	}
	
}
